import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp


class DBUser(db.Model):
  user = db.UserProperty()
  nickname = db.StringProperty(multiline=False)
  nickname_set = db.BooleanProperty()


class MainPage(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()

    if user:
      self.response.headers['Content-Type'] = 'text/html'
      self.response.out.write("""<html>
 <head>
  <title>Ricochet Robots 2.0</title>
  <style>
body {
  font-family: sans-serif;
  color: #cccccc;
  background-color: #000000;
}
a {
  color: #cccc00;
}
#title {
  font-size: 24px;
  color: #cccc00;
  width: 100%;
  text-align: center;
  padding: 0px 0px 25px 0px;
}
#gape {
  position: absolute;
  bottom: 5px;
  right: 5px;
}
  </style>
  <script language="Javascript">

function showEmail(email, name, shift) {
  var result = "";

  for(var index = 0; index < email.length; index++) {
    result += String.fromCharCode(email.charCodeAt(index) + shift);
  }

  document.write('<a href="mailto:' + result + '">' + name + '</a>');
}

  </script>
 </head>
 <body>
  <div id="title">
   Ricochet Robots 2.0
  </div>
  <div>
""")
      self.response.out.write('Welcome to Ricochet Robots 2.0, %s.  This site is currently under development.' % (user.nickname()))
      self.response.out.write("""<p />
   When Ricochet Robots 2.0 is live, you will be able to challenge your friends to a Web 2.0 version of Ricochet Robots.  The application will work using Google App Engine and the Google Web Toolkit in order to prevent the server issues that brought Ricochet Robots 1.0's demise.
   <p />
   When Ricochet Robots 2.5 is live, you will be able to play the Ricochet Robots daily challenge, using the same interface you've become familiar with in Ricochet Robots 2.0.
   <p />
   If you are interested in contributing to the development of either of these Ricochet Robots projects, please e-mail <script language="Javascript">showEmail("absqb^j=of`l`ebqol_lq+`lj", "our development team", 3);</script><noscript>--You must have JavaScript enabled to use this site--</noscript>.
   <p />
   Anticipated launch date: January 1, 2010
  </div>
  <div id="gape">
   <img src="http://code.google.com/appengine/images/appengine-silver-120x30.gif" alt="Powered by Google App Engine" />
  </div>
""")

      dbUser = DBUser.gql("WHERE user = :1", user)

      result = dbUser.fetch(1)

      print "Hi"
#      if result != None:
#        self.response.out.write(result.count())
#        self.response.out.write("Good")
#      else
#        self.response.out.write("Bad")

      self.response.out.write("""</body>
</html>""")
    else:
      self.redirect(users.create_login_url(self.request.uri))


def main():
  application = webapp.WSGIApplication(
    [('/', MainPage)],
    debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == "__main__":
  main()
